package org.mozhu.simple.rpc.server.network;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

public class RequestChannel {
    private Integer numProcessors;
    private Integer queueSize;
    private BlockingQueue<Request> requestQueue;
    private List<BlockingQueue<Response>> responseQueues;

    public RequestChannel(Integer numProcessors, Integer queueSize) {
        this.numProcessors = numProcessors;
        this.queueSize = queueSize;
        this.requestQueue = new ArrayBlockingQueue<>(queueSize);
        this.responseQueues = new ArrayList<>(numProcessors);
        for (int i = 0; i < numProcessors; i++) {
            this.responseQueues.add(new LinkedBlockingDeque<>());
        }
    }

    public Response receiveResponse(int id) {
        return null;
    }

    public static class Request {
        private int processor;
        private String connectionId;

        public Request(int processor, String connectionId) {
            this.processor = processor;
            this.connectionId = connectionId;
        }

        public String getConnectionId() {
            return connectionId;
        }

        public void setConnectionId(String connectionId) {
            this.connectionId = connectionId;
        }

        public int getProcessor() {
            return processor;
        }

        public void setProcessor(int processor) {
            this.processor = processor;
        }
    }

    public static class Response {
        private int processor;
        private Request request;
        private Send send;
        private ResponseAction responseAction;

        public Response(int processor, Request request, Send send, ResponseAction responseAction) {
            this.processor = processor;
            this.request = request;
            this.send = send;
            this.responseAction = responseAction;
        }

        public int getProcessor() {
            return processor;
        }

        public void setProcessor(int processor) {
            this.processor = processor;
        }

        public Request getRequest() {
            return request;
        }

        public void setRequest(Request request) {
            this.request = request;
        }

        public Send getSend() {
            return send;
        }

        public void setSend(Send send) {
            this.send = send;
        }

        public ResponseAction getResponseAction() {
            return responseAction;
        }

        public void setResponseAction(ResponseAction responseAction) {
            this.responseAction = responseAction;
        }
    }

    public enum ResponseAction {
        SendAction, NoOpAction, CloseConnectionAction
    }
}
